#include "DemoJsonCpp.h"
#include "json/json.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
using namespace Json;

void runDemoOfJsonCpp()
{
    /**
    [
        "liuwen",
        22,
        165,
        false,
        [ "summer", "xia" ],
        {
            "age" : 36,
            "name" : "xiazhihui"
        }
    ]
    */
    // 写数据
    Value root;
    root.append("liuwen");
    root.append(22);
    root.append(165);
    root.append(false);

    Value subValue;
    subValue.append("summer");
    subValue.append("xia");

    Value subObj;
    subObj["name"] = "xiazhihui";
    subObj["age"] = 36;

    root.append(subValue);
    root.append(subObj);

    string rootJson = root.toStyledString();

    cout << "格式化Json数据：" << endl;
    cout << rootJson << endl;

    FastWriter writer;
    cout << "压缩化Json数据：" << endl;
    cout << writer.write(root) << endl;

    ofstream ofs("d:/jsoncpp.json");
    ofs << writer.write(root);
    ofs.close();

    // 读数据
    ifstream ifs("d:/jsoncpp.json");
    Reader rd;
    Value readRoot;
    rd.parse(ifs, readRoot);

    cout << readRoot.toStyledString() << endl;

    if(readRoot.isArray()) 
    {
        for(int i = 0; i < readRoot.size(); ++i)
        {
            Value item = root[i];
            if(item.isInt()) 
            {
                cout << item.asInt() << ", ";
            }
            else if(item.isString()) 
            {
                cout << item.asString() << ", ";
            }
            else if(item.isBool()) 
            {
                cout << item.asBool() << ", ";
            }
            else if(item.isArray()) 
            {
                for(int j = 0; j < item.size(); ++j)
                {
                    cout << item[j].asString() << ", ";
                }
            }
            else if(item.isObject()) 
            {
                Value::Members keys = item.getMemberNames();
                for(int k = 0; k < keys.size(); ++k)
                {
                    // Value v = item[keys[k]];
                    // string s = v.isInt() ? to_string(v.asInt()) : v.asString();
                    cout << keys.at(k) << ":" << item[keys[k]] << ",";
                }
            }
        }

        cout << endl;
    } 
    else if(readRoot.isObject())
    {

    }


    // 对象 JSON 互转
    struct DataObj {
        DataObj(int x, int y) : x(x), y(y) {}
        int x, y;
    };

    FastWriter jsonWriter;

    DataObj data(100, 200);
    Value dataValue;
    dataValue["x"] = data.x;
    dataValue["y"] = data.y;
    cout << jsonWriter.write(dataValue) << endl;



    vector<DataObj> datas = { {1, 2}, {11, 22}, {111, 222}};
    Value dataValueArray;
    for(auto it = datas.begin(); it != datas.end(); ++it) 
    {
        Value itemValue;
        itemValue["x"] = it->x;
        itemValue["y"] = it->y;
        dataValueArray.append(itemValue);
    }
    cout << jsonWriter.write(dataValueArray) << endl;



    Value jsonObj;
    Reader jsonReader;
    jsonReader.parse(R"({"x":11,"y":22})", jsonObj);

    DataObj dataObj(jsonObj["x"].asInt(), jsonObj["y"].asInt());

    cout << dataObj.x << " : " << dataObj.y << endl;

}